package com.project.card.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.project.card.R;
import com.project.card.bean.NewsBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class NewsAdapter extends ArrayAdapter<NewsBean> {

    private Context context;
    private int mresource;

    private ArrayList<NewsBean> objects;
    public NewsAdapter(@NonNull Context context, int resource, @NonNull ArrayList<NewsBean> objects) {
        super(context, resource, objects);
        this.context=context;
        this.mresource=resource;
        this.objects=objects;
    }
    

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        convertView= LayoutInflater.from(this.context).inflate(this.mresource,parent,false);
        TextView title=convertView.findViewById(R.id.news_title);
        TextView date=convertView.findViewById(R.id.news_date);

        title.setText(getItem(position).getTitle());
        date.setText(new SimpleDateFormat("yyyy-MM-dd").format(getItem(position).getDate()));

        convertView.setOnClickListener(f ->{
            Uri uri = Uri.parse(objects.get(position).getLink());
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        });
        return convertView;
    }
    
    
}
